home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / GIFLIB12.ARJ / RAW2GIF.C < prev    next >
C/C++ Source or Header  |  1991-05-12  |  9KB  |  255 lines

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.1, Jun. 1989   *
  5. ******************************************************************************
  6. * Module to conver raw image into a GIF file.                     *
  7. * Options:                                                                   *
  8. * -q : quite printing mode.                             *
  9. * -s Width Height : specifies size of raw image.                             *
  10. * -p ColorMapFile : specifies color map for ray image (see gifclrmp).        *
  11. * -h : on line help.                                                         *
  12. ******************************************************************************
  13. * History:                                     *
  14. * 15 Oct 89 - Version 1.0 by Gershon Elber.                     *
  15. *****************************************************************************/
  16.  
  17. #ifdef __MSDOS__
  18. #include <dos.h>
  19. #include <alloc.h>
  20. #include <stdlib.h>
  21. #include <graphics.h>
  22. #include <io.h>
  23. #endif /* __MSDOS__ */
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <fcntl.h>
  28. #include "getarg.h"
  29. #include "gif_lib.h"
  30.  
  31. #define PROGRAM_NAME    "Raw2Gif"
  32.  
  33. #ifdef __MSDOS__
  34. extern unsigned int
  35.     _stklen = 16384;                 /* Increase default stack size. */
  36. #endif /* __MSDOS__ */
  37.  
  38. #ifdef SYSV
  39. static char *VersionStr =
  40.         "Gif library module,\t\tGershon Elber\n\
  41.     (C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  42. static char
  43.     *CtrlStr = "Raw2Gif q%- s!-Width|Height!d!d p%-ColorMapFile!s h%- RawFile!*s";
  44. #else
  45. static char
  46.     *VersionStr =
  47.     PROGRAM_NAME
  48.     GIF_LIB_VERSION
  49.     "    Gershon Elber,    "
  50.     __DATE__ ",   " __TIME__ "\n"
  51.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  52. static char
  53.     *CtrlStr =
  54.     PROGRAM_NAME
  55.     " q%- s!-Width|Height!d!d p%-ColorMapFile!s h%- RawFile!*s";
  56. #endif /* SYSV */
  57.  
  58. static GifColorType EGAPallete[] =      /* Default color map is EGA pallete. */
  59. {
  60.     {   0,   0,   0 },   /* 0. Black */
  61.     {   0,   0, 170 },   /* 1. Blue */
  62.     {   0, 170,   0 },   /* 2. Green */
  63.     {   0, 170, 170 },   /* 3. Cyan */
  64.     { 170,   0,   0 },   /* 4. Red */
  65.     { 170,   0, 170 },   /* 5. Magenta */
  66.     { 170, 170,   0 },   /* 6. Brown */
  67.     { 170, 170, 170 },   /* 7. LightGray */
  68.     {  85,  85,  85 },   /* 8. DarkGray */
  69.     {  85,  85, 255 },   /* 9. LightBlue */
  70.     {  85, 255,  85 },   /* 10. LightGreen */
  71.     {  85, 255, 255 },   /* 11. LightCyan */
  72.     { 255,  85,  85 },   /* 12. LightRed */
  73.     { 255,  85, 255 },   /* 13. LightMagenta */
  74.     { 255, 255,  85 },   /* 14. Yellow */
  75.     { 255, 255, 255 },   /* 15. White */
  76. };
  77. #define EGA_PALLETE_SIZE (sizeof(EGAPallete) / sizeof(GifColorType))
  78.  
  79. int Raw2Gif(int ImagwWidth, int ImagwHeight,
  80.                 GifColorType *ColorMap, int ColorMapSize);
  81. static int HandleGifError(GifFileType *GifFile);
  82.  
  83. /******************************************************************************
  84. * Interpret the command line, prepar global data and call the Gif routines.   *
  85. ******************************************************************************/
  86. void main(int argc, char **argv)
  87. {
  88.     int    Error, NumFiles, ImageWidth, ImageHeight, Dummy, Red, Green, Blue,
  89.     ColorMapSize, InFileHandle,
  90.     ImageSizeFlag = FALSE, ColorMapFlag = FALSE, HelpFlag = FALSE;
  91.     char **FileName = NULL, *ColorMapFile;
  92.     GifColorType *ColorMap;
  93.     FILE *InColorMapFile;
  94.  
  95.     if ((Error = GAGetArgs(argc, argv, CtrlStr, &GifQuitePrint,
  96.         &ImageSizeFlag, &ImageWidth, &ImageHeight,
  97.         &ColorMapFlag, &ColorMapFile,
  98.         &HelpFlag,
  99.         &NumFiles, &FileName)) != FALSE ||
  100.         (NumFiles > 1 && !HelpFlag)) {
  101.     if (Error)
  102.         GAPrintErrMsg(Error);
  103.     else if (NumFiles > 1)
  104.         GIF_MESSAGE("Error in command line parsing - one GIF file please.");
  105.     GAPrintHowTo(CtrlStr);
  106.     exit(1);
  107.     }
  108.  
  109.     if (HelpFlag) {
  110.     fprintf(stderr, VersionStr);
  111.     GAPrintHowTo(CtrlStr);
  112.     exit(0);
  113.     }
  114.  
  115.     if (ColorMapFlag) {
  116.     /* Read color map from given file: */
  117.     if ((InColorMapFile = fopen(ColorMapFile, "rt")) == NULL) {
  118.         GIF_MESSAGE("Failed to open COLOR MAP file (not exists!?).");
  119.         exit(2);
  120.     }
  121.     if ((ColorMap = (GifColorType *) 
  122.                     malloc(sizeof(GifColorType) * 255))  /* Biggest map. */
  123.         == NULL) {
  124.         GIF_MESSAGE("Failed to allocate bitmap, aborted.");
  125.         exit(3);
  126.     }
  127.  
  128.     for (ColorMapSize = 0;
  129.          ColorMapSize < 256 && !feof(InColorMapFile);
  130.          ColorMapSize++) {
  131.         fscanf(InColorMapFile, "%3d %3d %3d %3d\n",
  132.                         &Dummy, &Red, &Green, &Blue);
  133.         ColorMap[ColorMapSize].Red = Red;
  134.         ColorMap[ColorMapSize].Green = Green;
  135.         ColorMap[ColorMapSize].Blue = Blue;
  136.     }
  137.     }
  138.     else {
  139.     ColorMap = EGAPallete;
  140.     ColorMapSize = EGA_PALLETE_SIZE;
  141.     }
  142.  
  143.     if (NumFiles == 1) {
  144. #ifdef __MSDOS__
  145.     if ((InFileHandle = open(*FileName, O_RDONLY | O_BINARY)) == -1) {
  146. #else
  147.     if ((InFileHandle = open(*FileName, O_RDONLY)) == -1) {
  148. #endif /* __MSDOS__ */
  149.         GIF_MESSAGE("Failed to open RAW image file (not exists!?).");
  150.         exit(2);
  151.     }
  152.     dup2(InFileHandle, 0);               /* Make stdin from this file. */
  153.     }
  154.     else {
  155. #ifdef __MSDOS__
  156.     setmode(0, O_BINARY);          /* Make sure it is in binary mode. */
  157. #endif /* __MSDOS__ */
  158.     }
  159.  
  160. #ifdef __MSDOS__
  161.     setvbuf(stdin, NULL, _IOFBF, GIF_FILE_BUFFER_SIZE);
  162. #endif /* __MSDOS__ */
  163.  
  164.     /* Conver Raw image from stdin to Gif file in stdout: */
  165.     Raw2Gif(ImageWidth, ImageHeight, ColorMap, ColorMapSize);
  166. }
  167.  
  168. /******************************************************************************
  169. * Convert Raw image (One byte per pixel) into Gif file. Raw data is read from *
  170. * stdin, and Gif is dumped to stdout. ImagwWidth times ImageHeight bytes are  *
  171. * read. Color map is dumped from ColorMap.                      *
  172. ******************************************************************************/
  173. int Raw2Gif(int ImageWidth, int ImageHeight,
  174.                 GifColorType *ColorMap, int ColorMapSize)
  175. {
  176.     static int BitsPerPixelArray[] = { 2, 4 ,8, 16, 32, 64, 128, 256 };
  177.     int i, j, BitsPerPixel;
  178.     static GifPixelType *ScanLine;
  179.     GifFileType *GifFile;
  180.  
  181.     for (BitsPerPixel = 0;
  182.      BitsPerPixel < 8 && BitsPerPixelArray[BitsPerPixel] != ColorMapSize;
  183.      BitsPerPixel++);
  184.     if (++BitsPerPixel > 8) {
  185.     GIF_MESSAGE("Number of color map is NOT power of 2 up to 256.");
  186.     exit(3);
  187.     }
  188.  
  189.     if ((ScanLine = (GifPixelType *) malloc(sizeof(GifPixelType) * ImageWidth))
  190.                                 == NULL) {
  191.     GIF_MESSAGE("Failed to allocate scan line, aborted.");
  192.     exit(3);
  193.     }
  194.  
  195.     if ((GifFile = EGifOpenFileHandle(1)) == NULL) {       /* Gif to stdout. */
  196.     free((char *) ScanLine);
  197.     return HandleGifError(GifFile);
  198.     }
  199.  
  200.     if (EGifPutScreenDesc(GifFile, ImageWidth, ImageHeight, BitsPerPixel,
  201.               0, BitsPerPixel, ColorMap) == GIF_ERROR) {
  202.     free((char *) ScanLine);
  203.     return HandleGifError(GifFile);
  204.     }
  205.  
  206.     if (EGifPutImageDesc(GifFile, 0, 0, ImageWidth, ImageHeight, FALSE, 1,
  207.              NULL) == GIF_ERROR) {
  208.     free((char *) ScanLine);
  209.     return HandleGifError(GifFile);
  210.     }
  211.  
  212.     /* Here it is - get one raw line from stdin, and dump to stdout Gif: */
  213.     GifQprintf("\n%s: Image 1 at (0, 0) [%dx%d]:     ",
  214.     PROGRAM_NAME, ImageWidth, ImageHeight);
  215.     for (i = 0; i < ImageHeight; i++) {
  216.     /* Note we assume here PixelSize == Byte, which is not necessarily   */
  217.     /* so. If not - must read one byte at a time, and coerce to pixel.   */
  218.     if (fread(ScanLine, 1, ImageWidth, stdin) != ImageWidth) {
  219.         GIF_MESSAGE("RAW input file ended prematurely.");
  220.         exit(3);
  221.     }
  222.  
  223.     for (j = 0; j < ImageWidth; j++)
  224.         if (ScanLine[j] >= ColorMapSize)
  225.         GIF_MESSAGE("Warning: RAW data color > maximum color map entry.");
  226.  
  227.     if (EGifPutLine(GifFile, ScanLine, ImageWidth) == GIF_ERROR) {
  228.         free((char *) ScanLine);
  229.         return HandleGifError(GifFile);
  230.     }
  231.     GifQprintf("\b\b\b\b%-4d", i);
  232.     }
  233.  
  234.     if (EGifCloseFile(GifFile) == GIF_ERROR) {
  235.     free((char *) ScanLine);
  236.     return HandleGifError(GifFile);
  237.     }
  238.  
  239.     free((char *) ScanLine);
  240.     return 0;
  241. }
  242.  
  243. /******************************************************************************
  244. * Handle last GIF error. Try to close the file and free all allocated memory. *
  245. ******************************************************************************/
  246. static int HandleGifError(GifFileType *GifFile)
  247. {
  248.     int i = GifLastError();
  249.  
  250.     if (EGifCloseFile(GifFile) == GIF_ERROR) {
  251.     GifLastError();
  252.     }
  253.     return i;
  254. }
  255.